//
//  UIViewController+NEDBase.m
//  NEDBase
//
//  Created by 98du on 2021/12/3.
//

#import "UIViewController+NEDBase.h"
#import "UIColor+NEDStyle.h"
#import "NEDConstant+Method.h"
#import "UINavigationBar+NED.h"
#import "UIFont+NED.h"
#import "NEDConstant.h"
#import "NEDConstant+Method.h"
#import "UIViewController+NEDNaivigationItem.h"
#import "NSObject+NEDDealloc.h"
#import "NEDApp+Mediator.h"

@implementation UIViewController (NEDBase)

+(void)load{
    ned_swizzleMethod(self.class, @selector(viewWillAppear:), @selector(ned_baseViewWillAppear:));
//    ned_swizzleMethod(self.class, @selector(viewWillDisappear:), @selector(ned_baseViewWillDisappear:));
    
    ned_swizzleMethod(self.class, @selector(viewDidLoad), @selector(ned_baseViewDidLoad));
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - swizzleMethod

- (void)ned_baseViewDidLoad{
    if(!ned_mainBundleClass(self.class)){
        [self ned_baseViewDidLoad];
        return;
    }
    self.view.backgroundColor = [UIColor ned_background];
    self.edgesForExtendedLayout = [self ned_edgesForExtendedLayout];
    self.view.clipsToBounds = YES;
    
    [self __nedAddNavigationBackItem];
    
    [self ned_baseViewDidLoad];
    
    [self ned_addContentView];
    [self ned_addNotification];
    
    WS(weakSelf)
    [self ned_deallocBlock:^(Class  _Nullable __unsafe_unretained cname) {
        NSLog(@"🧹 dealloc: %@",NSStringFromClass(cname));
        if(APP.mediator && [APP.mediator.delegateHUD respondsToSelector:@selector(hud_hiddenLoading)]){
            [APP.mediator.delegateHUD hud_hiddenLoading];
        }
        [[NSNotificationCenter defaultCenter] removeObserver:weakSelf];
    }];
    
//    [self ned_navigationBarShowStyleUpdate];
    
    NSLog(@"👀 showVC: %@",NSStringFromClass(self.class));
}


- (void)ned_baseViewWillAppear:(BOOL)animated{
    if(!ned_mainBundleClass(self.class)){
        [self ned_baseViewWillAppear:animated];
        return;
    }
    [self ned_baseViewWillAppear:animated];
    
    [self ned_navigationBarShowStyleUpdate];
    
//    for(UIView *aView in self.navigationController.navigationBar.subviews) {
//        if ([aView isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]) {
//            if(self.ned_navcIsTransucent){
//                aView.hidden = YES;
//            }else{
//                aView.hidden = NO;
//            }
//        }
//    }
//
//    if(self.ned_navcIsTransucent){
//        NSArray * subviews = self.navigationController.navigationBar.subviews;
//        for (UIView *subview in subviews) {
//            subview.backgroundColor = UIColor.clearColor;
//            for(UIView * ss in subview.subviews){
//                ss.backgroundColor = UIColor.clearColor;
//                for(UIView * sss in ss.subviews){
//                    sss.backgroundColor = UIColor.clearColor;
//                    for(UIView * ssss in sss.subviews){
//                        ssss.backgroundColor = UIColor.clearColor;
//                    }
//                }
//            }
//        }
//    }
}
#pragma mark - privite

- (void)__nedAddNavigationBackItem{
    if([self ned_navcHiddenBackBarItem]){
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
        return;
    }
    if (self.navigationController){
        // 不是rootViewController
        if (![self isEqual:self.navigationController.viewControllers.firstObject]){
            WS(weakSelf)
            UIImage *back = [self ned_navcBackItemImage];
            NSMutableArray *icon = [NSMutableArray array];
            if (back) {
                [icon addObject:back];
            } else {
                [icon addObject:@"返回"];
            }
            [self ned_setNavLeftItems:icon handle:^(UIButton * _Nonnull btn, NSInteger index) {
                [weakSelf ned_navcBackEvent];
            } btnConfig:nil];
        }
    }
}


#pragma mark - NEDBaseViewControllerNaviationBarDelegate


/// 导航条颜色
- (UIColor *)ned_navcBackgroundColor{
    if([self ned_navcIsTransucent]){
        return UIColor.clearColor;
    }
    return nil;
}

- (UIImage *)ned_navcBackgroundImage {
    return nil;
}
/// 导航条字体颜色
- (UIColor *)ned_navcTitleColor{
    return nil;
}
/// 导航条字体大小
- (UIFont *)ned_navcTitleFont{
    return [UIFont ned_systemMediumSize:18];
}
/// 不显示返回按钮
- (BOOL)ned_navcHiddenBackBarItem{
    return NO;
}
/// 导航条透明
- (BOOL)ned_navcIsTransucent{
    return NO;
}
/// 显示大标题
- (BOOL)ned_navcShowLargeTitle{
    return NO;
}

- (void)ned_navigationBarShowStyleUpdate{
         
    if(!self.navigationController){
        return;
    }
    /// JXPagerViewList 可能有这问题 他没有自身的
    if(![self.navigationController.viewControllers containsObject:self]){
        return;
    }
    
    NSLog(@"self.navigationController == %@",self.navigationController);
    
    self.edgesForExtendedLayout = [self ned_edgesForExtendedLayout];
    
    UIColor * titleColor = self.ned_navcTitleColor?:[UIColor ned_navigationTitle];
    UIColor * navigationBackgroundColor = self.ned_navcBackgroundColor?:UIColor.ned_navigationBackground;
    UIImage * bavBackgroundImg = self.ned_navcBackgroundImage;
    NSDictionary * titleTextAttributes = @{
        NSFontAttributeName: [self ned_navcTitleFont],
        NSForegroundColorAttributeName:titleColor};
    
    if (@available(iOS 13.0, *)) {
        
        UINavigationBarAppearance * appearance = [UINavigationBarAppearance new];
        
        if([self ned_navcIsTransucent]){
            self.navigationController.navigationBar.translucent = YES;
//            self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
            
            [appearance configureWithTransparentBackground];
            appearance.backgroundColor = [UIColor clearColor];
            appearance.backgroundEffect = nil;
        }else{
            self.navigationController.navigationBar.translucent = NO;
//            self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
            
            [appearance configureWithOpaqueBackground];
        }
        appearance.titleTextAttributes = titleTextAttributes;
        appearance.backgroundImage = bavBackgroundImg ?: [UIImage new];
        //导航条样式设置
        appearance.backgroundColor = navigationBackgroundColor;
        appearance.shadowColor = [UIColor clearColor];
        
        self.navigationController.navigationBar.standardAppearance = appearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
        
    }else{
        if([self ned_navcIsTransucent]){
            self.navigationController.navigationBar.translucent = YES;
            self.navigationController.navigationBar.tintColor = UIColor.clearColor;
            [self.navigationController.navigationBar setBackgroundImage:bavBackgroundImg ?: [UIImage new] forBarMetrics:UIBarMetricsDefault];
        }else{
            self.navigationController.navigationBar.translucent = NO;
            [self.navigationController.navigationBar setBackgroundImage:bavBackgroundImg ?: [navigationBackgroundColor ned_drawImageWithColor] forBarMetrics:UIBarMetricsDefault];
        }
        
        self.navigationController.navigationBar.shadowImage = [UIImage new];
        [self.navigationController.navigationBar setTitleTextAttributes:titleTextAttributes];
    }
    
    if (@available(iOS 11.0, *)) {
        if(self.ned_navcShowLargeTitle){
            self.navigationController.navigationBar.prefersLargeTitles = YES;
            self.navigationController.navigationItem.largeTitleDisplayMode =  UINavigationItemLargeTitleDisplayModeAutomatic;
        }else{
            self.navigationController.navigationBar.prefersLargeTitles = NO;
            self.navigationController.navigationItem.largeTitleDisplayMode =  UINavigationItemLargeTitleDisplayModeNever;
        }
    }
    [self setNeedsStatusBarAppearanceUpdate];
}

/// 改变导航条背景色
- (void)ned_navcChangeBackgroundColor:(UIColor *)changeColor{
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance * appearance = self.navigationController.navigationBar.standardAppearance;
        [appearance configureWithTransparentBackground];
        appearance.shadowColor = [UIColor clearColor];
        appearance.backgroundColor = changeColor;
        
        self.navigationController.navigationBar.standardAppearance = appearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
    }else{
        [self.navigationController.navigationBar setBackgroundImage:[changeColor ned_drawImageWithColor] forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = [UIImage new];
    }
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)ned_navcChangeTitleColor:(UIColor *)changeColor{
    NSDictionary * attrs = @{
        NSFontAttributeName: [self ned_navcTitleFont],
        NSForegroundColorAttributeName:changeColor};
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance * appearance = self.navigationController.navigationBar.standardAppearance;
        appearance.titleTextAttributes = attrs;
        self.navigationController.navigationBar.standardAppearance = appearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
    }else{
        [self.navigationController.navigationBar setTitleTextAttributes:attrs];
    }
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)ned_navcChangeBackItemImage:(UIImage *)backImage{
    if(IsNull(backImage)){
        return;
    }
    WS(weakSelf)
    [self ned_setNavLeftItems:@[backImage] handle:^(UIButton * _Nonnull btn, NSInteger index) {
        [weakSelf ned_navcBackEvent];
    } btnConfig:nil];
}

- (UIImage *)ned_navcBackItemImage{
    if(APP.mediator.delegateStyle &&
       [APP.mediator.delegateStyle respondsToSelector:@selector(style_viewcontrollerBackBarItemImageName)]){
        return [UIImage imageNamed:[APP.mediator.delegateStyle style_viewcontrollerBackBarItemImageName]];
    }
    return [UIImage imageNamed:@"ic_back_black"];
}

- (void)ned_navcBackEvent{
    [self ned_viewcontrollerClose];
}

#pragma mark - NEDBaseViewControllerDelegate

/// 控制器关闭
- (void)ned_viewcontrollerClose{
    if (self.navigationController &&
        [self isEqual:self.navigationController.topViewController]){
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)ned_addContentView{}
- (void)ned_addNotification{}

/// UIRectEdgeAll
- (UIRectEdge)ned_edgesForExtendedLayout{
    if([self ned_navcIsTransucent]){
        return UIRectEdgeAll;
    }
    return UIRectEdgeNone;
}

/// view的背景图
- (void)ned_viewBackgroundImage:(UIImage *)backgroundImage{
    if(backgroundImage){
        self.view.layer.contents = (id)backgroundImage.CGImage;
    }else{
        self.view.layer.contents = nil;
    }
}

- (void)ned_addContentMainViewInCategory {
    
}

- (void)ned_addContentNavigationViewInCategory {
    
}
- (BOOL)willDealloc{
    return YES;
}
@end



